/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package repositoriodistribuido;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ylopez
 */
public class dcli {

    private static final int PORTDREP = 43718;

    private static int evaluateArgument(String [] args){
        int argumentConfig = 0;

        if (args.length == 12) {
            if (
                args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-d") &&
                args[6].equals("-a") && args[8].equals("-n") && args[10].equals("-c")
               ) {
                argumentConfig = 121;
            } else {
                argumentConfig = 1;
            }
        }else if (args.length == 10) {
            if (
                args[0].equals("-p") && args[2].equals("-d") && args[4].equals("-a") &&
                args[6].equals("-n") && args[8].equals("-c")
               ) {
                argumentConfig = 101;
            } else if (
                    args[0].equals("-h") && args[2].equals("-d") && args[4].equals("-a") &&
                    args[6].equals("-n") && args[8].equals("-c")
                      ) {
                argumentConfig = 102;
            } else if (
                    args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-a") &&
                    args[6].equals("-n") && args[8].equals("-c")
                      ) {
                argumentConfig = 103;
            } else if (
                    args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-d") &&
                    args[6].equals("-n") && args[8].equals("-c")
                      ) {
                argumentConfig = 104;
            } else if (
                    args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-d") &&
                    args[6].equals("-a") && args[8].equals("-c")
                      ) {
                argumentConfig = 105;
            } else if (
                    args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-d") &&
                    args[6].equals("-a") && args[8].equals("-n")
                      ) {
                argumentConfig = 106;
            } else {
                argumentConfig = 1;
            }
        } else if (args.length == 8) {
            if (
                 args[0].equals("-d") && args[2].equals("-a") && args[4].equals("-n") &&
                 args[6].equals("-c")
               ) {
                argumentConfig = 81;
            } else if (
                 args[0].equals("-p") && args[2].equals("-a") && args[4].equals("-n") &&
                 args[6].equals("-c")
               ) {
                argumentConfig = 82;
            } else if (
                 args[0].equals("-p") && args[2].equals("-d") && args[4].equals("-n") &&
                 args[6].equals("-c")
               ) {
                argumentConfig = 83;
            } else if (
                 args[0].equals("-p") && args[2].equals("-d") && args[4].equals("-a") &&
                 args[6].equals("-c")
               ) {
                argumentConfig = 84;
            } else if (
                 args[0].equals("-p") && args[2].equals("-d") && args[4].equals("-a") &&
                 args[6].equals("-n")
               ) {
                argumentConfig = 85;
            } else if (
                    args[0].equals("-h") && args[2].equals("-a") && args[4].equals("-n") &&
                    args[6].equals("-c")
                      ){
                argumentConfig = 86;
            } else if (
                    args[0].equals("-h") && args[2].equals("-d") && args[4].equals("-n") &&
                    args[6].equals("-c")
                      ){
                argumentConfig = 87;
            } else if (
                    args[0].equals("-h") && args[2].equals("-d") && args[4].equals("-a") &&
                    args[6].equals("-c")
                      ){
                argumentConfig = 88;
            } else if (
                    args[0].equals("-h") && args[2].equals("-d") && args[4].equals("-a") &&
                    args[6].equals("-n")
                      ){
                argumentConfig = 89;
            } else if (
                    args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-n") &&
                    args[6].equals("-c")
                      ){
                argumentConfig = 810;
            } else if (
                    args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-a") &&
                    args[6].equals("-c")
                      ){
                argumentConfig = 811;
            } else if (
                    args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-a") &&
                    args[6].equals("-n")
                      ){
                argumentConfig = 812;
            } else if (
                    args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-d") &&
                    args[6].equals("-c")
                      ) {
                argumentConfig = 813;
            } else if (
                    args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-d") &&
                    args[6].equals("-n")
                      ) {
                argumentConfig = 814;
            } else if (
                    args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-d") &&
                    args[6].equals("-a")
                      ) {
                argumentConfig = 815;
            } else {
                argumentConfig = 1;
            }
        } else if (args.length == 6) {
            if (args[0].equals("-a") && args[2].equals("-n") && args[4].equals("-c")) {
                argumentConfig = 61;
            } else if (args[0].equals("-d") && args[2].equals("-n") && args[4].equals("-c")) {
                argumentConfig = 62;
            } else if (args[0].equals("-d") && args[2].equals("-a") && args[4].equals("-c")) {
                argumentConfig = 63;
            } else if (args[0].equals("-d") && args[2].equals("-a") && args[4].equals("-n")) {
                argumentConfig = 64;
            } else if (args[0].equals("-p") && args[2].equals("-n") && args[4].equals("-c")) {
                argumentConfig = 65;
            } else if (args[0].equals("-p") && args[2].equals("-a") && args[4].equals("-c")) {
                argumentConfig = 66;
            } else if (args[0].equals("-p") && args[2].equals("-a") && args[4].equals("-n")) {
                argumentConfig = 67;
            } else if (args[0].equals("-p") && args[2].equals("-d") && args[4].equals("-c")) {
                argumentConfig = 68;
            } else if (args[0].equals("-p") && args[2].equals("-d") && args[4].equals("-n")) {
                argumentConfig = 69;
            } else if (args[0].equals("-p") && args[2].equals("-d") && args[4].equals("-a")) {
                argumentConfig = 610;
            } else if (args[0].equals("-h") && args[2].equals("-n") && args[4].equals("-c")) {
                argumentConfig = 611;
            } else if (args[0].equals("-h") && args[2].equals("-a") && args[4].equals("-c")) {
                argumentConfig = 612;
            } else if (args[0].equals("-h") && args[2].equals("-a") && args[4].equals("-n")) {
                argumentConfig = 613;
            } else if (args[0].equals("-h") && args[2].equals("-d") && args[4].equals("-c")) {
                argumentConfig = 614;
            } else if (args[0].equals("-h") && args[2].equals("-d") && args[4].equals("-n")) {
                argumentConfig = 615;
            }  else if (args[0].equals("-h") && args[2].equals("-d") && args[4].equals("-a")) {
                argumentConfig = 616;
            }  else if (args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-d")) {
                argumentConfig = 617;
            } else {
                argumentConfig = 1;
            }
        } else if (args.length == 4) {
            if (args[0].equals("-n") && args[2].equals("-c")) {
                argumentConfig = 41;
            } else if (args[0].equals("-a") && args[2].equals("-c")) {
                argumentConfig = 42;
            } else if (args[0].equals("-a") && args[2].equals("-n")) {
                argumentConfig = 43;
            } else if (args[0].equals("-d") && args[2].equals("-c")) {
                argumentConfig = 44;
            } else if (args[0].equals("-d") && args[2].equals("-n")) {
                argumentConfig = 45;
            } else if (args[0].equals("-p") && args[2].equals("-c")) {
                argumentConfig = 46;
            } else if (args[0].equals("-p") && args[2].equals("-n")) {
                argumentConfig = 47;
            } else if (args[0].equals("-p") && args[2].equals("-a")) {
                argumentConfig = 48;
            } else if (args[0].equals("-p") && args[2].equals("-d")) {
                argumentConfig = 49;
            } else if (args[0].equals("-h") && args[2].equals("-c")) {
                argumentConfig = 410;
            } else if (args[0].equals("-h") && args[2].equals("-n")) {
                argumentConfig = 411;
            } else if (args[0].equals("-h") && args[2].equals("-a")) {
                argumentConfig = 412;
            } else if (args[0].equals("-h") && args[2].equals("-d")) {
                argumentConfig = 413;
            } else if (args[0].equals("-h") && args[2].equals("-p")) {
                argumentConfig = 414;
            } else {
                argumentConfig = 1;
            }
        } else if (args.length == 2) {
            if (args[0].equals("-h")) {
                argumentConfig = 11;
            } else if (args[0].equals("-p")) {
                argumentConfig = 12;
            } else if (args[0].equals("-d")) {
                argumentConfig = 13;
            } else if (args[0].equals("-a")) {
                argumentConfig = 14;
            } else if (args[0].equals("-n")) {
                argumentConfig = 15;
            } else if (args[0].equals("-c")) {
                argumentConfig = 16;
            } else {
                argumentConfig = 1;
            }
        }

        return argumentConfig;
    }

    public static void main(String[] args) throws FileNotFoundException, IOException {

        String hostDrep = "127.0.0.1";
        int portDrep = PORTDREP;
        String directory = System.getProperty("user.dir");
        String file = null;
        String userName = System.getProperty("user.name");
        String configFile = null;
        String[] parameter = null;

        if (args.length == 2 && args[0].equals("-c")) {
            parameter = dcliCommands.getParameter(configFile);
        } else if (args.length == 4 && args[2].equals("-c")) {
            parameter = dcliCommands.getParameter(configFile);
        } else if (args.length == 6 && args[4].equals("-c")) {
            parameter = dcliCommands.getParameter(configFile);
        } else if (args.length == 8 && args[6].equals("-c")) {
            parameter = dcliCommands.getParameter(configFile);
        } else if (args.length == 10 && args[8].equals("-c")) {
            parameter = dcliCommands.getParameter(configFile);
        } else if (args.length == 12 && args[10].equals("-c")) {
            parameter = dcliCommands.getParameter(configFile);
        }

        if (configFile != null) {
            if (!parameter[0].equals("vacio")) {
                userName = parameter[0];
            }
            if (!parameter[1].equals("vacio")) {
                hostDrep = parameter[1];
            }
            if (!parameter[2].equals("vacio")) {
                portDrep = Integer.parseInt(parameter[2]);
            }
            if (!parameter[3].equals("vacio")) {
                directory = parameter[3];
            }
            if (!parameter[4].equals("vacio")) {
                file = parameter[4];
            }
        }

        try {
            switch (evaluateArgument(args)) {
                case 0:
                    break;
                case 1:
                    System.err.println("Llamada a dcli inva'lida");
                    System.exit(-1);
                case 11:
                    hostDrep = args[1]; break;
                case 12:
                    portDrep = Integer.parseInt(args[1]); break;
                case 13:
                    directory = args[1]; break;
                case 14:
                    file = args[1]; break;
                case 15:
                    userName = args[1]; break;
                case 16:
                    configFile = args[1];
                    break;
                case 41:
                    userName = args[1]; configFile = args[3];
                    
                    break;
                case 42:
                    file = args[1]; configFile = args[3];
                    
                    break;
                case 43:
                    file = args[1]; userName = args[3]; break;
                case 44:
                    directory = args[1]; configFile = args[3];
                    
                    break;
                case 45:
                    directory = args[1]; userName = args[3]; break;
                case 46:
                    portDrep = Integer.parseInt(args[1]); configFile = args[3];
                    
                    break;
                case 47:
                    portDrep = Integer.parseInt(args[1]); userName = args[3]; break;
                case 48:
                    portDrep = Integer.parseInt(args[1]); file = args[3]; break;
                case 49:
                    portDrep = Integer.parseInt(args[1]); directory = args[3]; break;
                case 61:
                    file = args[1]; userName = args[3]; configFile = args[5];
                    
                    break;
                case 62:
                    directory = args[1]; userName = args[3]; configFile = args[5];
                    
                    break;
                case 63:
                    directory = args[1]; file = args[3]; configFile = args[5];
                    
                    break;
                case 64:
                    directory = args[1]; file = args[3]; userName = args[5]; break;
                case 65:
                    portDrep = Integer.parseInt(args[1]); userName = args[3]; configFile = args[5];
                    
                    break;
                case 66:
                    portDrep = Integer.parseInt(args[1]); file = args[3]; configFile = args[5];
                    
                    break;
                case 67:
                    portDrep = Integer.parseInt(args[1]); file = args[3]; userName = args[5]; break;
                case 68:
                    portDrep = Integer.parseInt(args[1]); directory = args[3]; configFile = args[5];
                    
                    break;
                case 69:
                    portDrep = Integer.parseInt(args[1]); directory = args[3]; userName = args[5]; break;
                case 81:
                    directory = args[1]; file = args[3]; userName = args[5];
                    configFile = args[7];
                    
                    break;
                case 82:
                    portDrep = Integer.parseInt(args[1]); file = args[3]; userName = args[5];
                    configFile = args[7];
                    
                    break;
                case 83:
                    portDrep = Integer.parseInt(args[1]); directory = args[3]; userName = args[5];
                    configFile = args[7];
                    
                    break;
                case 84:
                    portDrep = Integer.parseInt(args[1]); directory = args[3]; file = args[5];
                    configFile = args[7];
                    
                    break;
                case 85:
                    portDrep = Integer.parseInt(args[1]); directory = args[3]; file = args[5];
                    userName = args[7]; break;
                case 86:
                    hostDrep = args[1]; file = args[3]; userName = args[5];
                    configFile = args[7];
                    
                    break;
                case 87:
                    hostDrep = args[1]; directory = args[3]; userName = args[5];
                    configFile = args[7];
                    
                    break;
                case 88:
                    hostDrep = args[1]; directory = args[3]; file = args[5];
                    configFile = args[7];
                    
                    break;
                case 89:
                    hostDrep = args[1]; directory = args[3]; file = args[5];
                    userName = args[7]; break;
                case 101:
                    portDrep = Integer.parseInt(args[1]); directory = args[3]; file = args[5];
                    userName = args[7]; configFile = args[9];
                    
                    break;
                case 102:
                    hostDrep = args[1]; directory = args[3]; file = args[5];
                    userName = args[7]; configFile = args[9];
                    
                    break;
                case 103:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); file = args[5];
                    userName = args[7]; configFile = args[9];
                    
                    break;
                case 104:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); directory = args[5];
                    userName = args[7]; configFile = args[9];
                    
                    break;
                case 105:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); directory = args[5];
                    file = args[7]; configFile = args[9];
                    
                    break;
                case 106:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); directory = args[5];
                    file = args[7]; userName = args[9]; break;
                case 121:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); directory = args[5];
                    file = args[7]; userName = args[9]; configFile = args[11];
                    
                    break;
                case 410:
                    hostDrep = args[1]; configFile = args[3];
                    
                    break;
                case 411:
                    hostDrep = args[1]; userName = args[3]; break;
                case 412:
                    hostDrep = args[1]; file = args[3]; break;
                case 413:
                    hostDrep = args[1]; directory = args[3]; break;
                case 414:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); break;
                case 610:
                    portDrep = Integer.parseInt(args[1]); directory = args[3]; file = args[5];
                    break;
                case 611:
                    hostDrep = args[1]; userName = args[3]; configFile = args[5];
                    
                    break;
                case 612:
                    hostDrep = args[1]; file = args[3]; configFile = args[5];
                    
                    break;
                case 613:
                    hostDrep = args[1]; file = args[3]; userName = args[5];
                    break;
                case 614:
                    hostDrep = args[1]; directory = args[3]; configFile = args[5];
                    
                    break;
                case 615:
                    hostDrep = args[1]; directory = args[3]; userName = args[5];
                    break;
                case 616:
                    hostDrep = args[1]; directory = args[3]; file = args[5];
                    break;
                case 617:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); directory = args[5];
                    break;
                case 810:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); userName = args[5];
                    configFile = args[7]; break;
                case 811:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); file = args[5];
                    configFile = args[7]; 
                    break;
                case 812:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); file = args[5];
                    userName = args[7]; break;
                case 813:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); directory = args[5];
                    configFile = args[7]; 
                    break;
                case 814:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); directory = args[5];
                    userName = args[7]; break;
                case 815:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); directory = args[5];
                    file = args[7]; break;
            }
        } catch (NumberFormatException nfe) {
            //TODO Manejar esta excepcion bien
            System.err.println("El puerto no es un nu'mero entero.");
            nfe.printStackTrace();
            System.exit(-1);
        }

        System.out.println("Host Drep: " + hostDrep);
        System.out.println("Puerto Drep: " + portDrep);
        System.out.println("Directorio: " + directory);
        System.out.println("Nombre usuario: " + userName);

        drepCommands c = null;
        Date connectionDate = new Date();
        String thisUser = userName + "  " + connectionDate.toString();
        dcliCommands.setCurrentDirectory(directory);
        try {
            c = (drepCommands) Naming.lookup("rmi://"+ hostDrep +":"+ portDrep +"/DrepService");
            c.connectionDrep(userName, connectionDate);

            if (file != null) {
                dcliCommands.readCommandsFromFile(file, c);
            }

            System.out.println("Escriba Bye para salir");
            System.out.print(userName + "$> ");
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            String fromUser;
            String[] commandLine;
            String[] results;
            try {
                fromUser = stdIn.readLine();
                while (fromUser != null) {
                    try {
                        commandLine = fromUser.split(" ");
                        if (commandLine[0].equals("ldir")) {
                            results = dcliCommands.ldir(directory);
                            dcliCommands.printResoults("ldir", results);
                        } else if (commandLine[0].equals("rdir")) {
                            results = c.rdir();
                            dcliCommands.printResoults("rdir", results);
                        } else if (commandLine[0].equals("tuser")) {
                            results = c.tuser();
                            dcliCommands.printResoults("tuser", results);
                        } else if (commandLine[0].equals("nrep")) {
                            results = c.nrep();
                            dcliCommands.printResoults("nrep", results);
                        } else if (commandLine[0].equals("sube")) {
                            results = dcliCommands.sube(commandLine[1],c);
                            dcliCommands.printResoults("sube", results);
                        } else if (commandLine[0].equals("baja")) {
                            results = dcliCommands.baja(commandLine[1],c);
                            dcliCommands.printResoults("baja", results);
                        } else if (commandLine[0].equals("cat")) {
                            dcliCommands.cat(commandLine[1]);
                        } else if (commandLine[0].equals("Bye")) {
                            c.disconnectDrep(thisUser);
                            break;
                        } else {
                            System.err.println("Comando: "+ commandLine[0] +" invalido.");
                        }
                        System.out.print(userName + "$> ");
                        fromUser = stdIn.readLine();
                    } catch (RemoteException e) {
                        System.out.println ( "RemoteException");
                        System.exit(0);
                    }
                }
                stdIn.close();
            } catch (IOException ex) {
                Logger.getLogger(dcli.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (MalformedURLException murle ) {
            System.out.println ();
            System.out.println("MalformedURLException");
            System.exit(0);
            //System.out.println ( murle );
        } catch (RemoteException re) {
            System.out.println ();
            System.out.println ( "RemoteException");
            System.exit(0);
//            System.out.println (re);
        } catch (NotBoundException nbe) {
            System.out.println ();
            System.out.println ("NotBoundException");
            System.exit(0);
//            System.out.println (nbe);
        } finally {
            try {
                c.disconnectDrep(thisUser);
            } catch (RemoteException ex) {
                Logger.getLogger(dcli.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
